package com.pyl.admin.system.controller;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.pyl.admin.core.enums.ResultEnum;
import com.pyl.admin.core.exception.ResultException;
import com.pyl.admin.core.web.FileUpload;
import com.pyl.admin.system.entity.SysFile;
import com.pyl.admin.system.service.SysFileService;
import com.pyl.core.utils.ResultVoUtil;
import com.pyl.core.vo.ResultVo;

import cn.hutool.core.util.ObjectUtil;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 灵魂贰婶
 * @since 2019-06-28
 */
@Controller
public class UploadController {

	@Autowired
	private SysFileService sysFileService;

	/**
	 * 上传web格式图片
	 */
	@PostMapping("/upload/image")
	@ResponseBody
	public ResultVo<?> uploadImage(@RequestParam("image") MultipartFile multipartFile) {

		// 创建Upload实体对象
		SysFile upload = FileUpload.getFile(multipartFile, "/images");
		try {
			return saveImage(multipartFile, upload);
		} catch (IOException | NoSuchAlgorithmException e) {
			return ResultVoUtil.error("上传图片失败");
		}
	}

	/**
	 * 上传web格式头像
	 */
	@PostMapping("/upload/picture")
	@ResponseBody
	public ResultVo<?> uploadPicture(@RequestParam("picture") MultipartFile multipartFile) {

		// 创建Upload实体对象
		SysFile upload = FileUpload.getFile(multipartFile, "/picture");
		try {
			return saveImage(multipartFile, upload);
		} catch (IOException | NoSuchAlgorithmException e) {
			return ResultVoUtil.error("上传头像失败");
		}
	}

	/**
	 * 保存上传的web格式图片
	 */
	private ResultVo<?> saveImage(MultipartFile multipartFile, SysFile upload)
			throws IOException, NoSuchAlgorithmException {
		// 判断是否为支持的图片格式
		String[] types = { "image/gif", "image/jpg", "image/jpeg", "image/png" };
		if (!FileUpload.isContentType(multipartFile, types)) {
			throw new ResultException(ResultEnum.NO_FILE_TYPE);
		}

		// 判断图片是否存在
		SysFile isFile = sysFileService
				.selectOne(new EntityWrapper<SysFile>().eq("sha1", FileUpload.getFileSHA1(multipartFile)));
		if (ObjectUtil.isNotNull(isFile)) {
			return ResultVoUtil.success(isFile);
		}
		FileUpload.transferTo(multipartFile, upload);
		// 将文件信息保存到数据库中
		sysFileService.insert(upload);
		return ResultVoUtil.success(upload);
	}

	/**
	 * 上传文件图片
	 * @param multipartFile
	 * @return
	 */
	@PostMapping("/upload/file")
	@ResponseBody
	public ResultVo<?> uploadFile(@RequestParam("file") MultipartFile multipartFile) {

		// 创建Upload实体对象
		SysFile upload = FileUpload.getFile(multipartFile, "/file");
		try {
			return saveImage(multipartFile, upload);
		} catch (IOException | NoSuchAlgorithmException e) {
			return ResultVoUtil.error("上传文件失败");
		}
	}
	
	
	
}
